package com.aipolice.web;

import com.aipolice.pojo.User;
import com.aipolice.service.UserService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.PostRemove;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;



    @GetMapping("/users")
    public List<User> users(){
        return userService.list();

    }

    @DeleteMapping("/users/{id}")
    public void delete(@PathVariable("id") int id){
        System.out.println("删除方法");
        userService.delete(id);
    }

    @GetMapping("/users/{id}")
    public User get(@PathVariable("id") int id){
        System.out.println("查询");
        return userService.find(id);
    }

    @PostMapping("/users")
    public int add(User user){
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        user.setUpdateTime(timestamp);
//        if(user.getId() != -1){
//            userService.
//            return 2;
//        }
        user.setDel(0);

        user.setCreateTime(timestamp);
        System.out.println(user.toString());
        userService.add(user);
        int result = 1;
        return result;
    }
}
